package othello.logic;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class MoveQueue
 * Holds the next move for human players.
 */
public class MoveQueue {

    private Coordinates nextMove;
    Lock lock = new ReentrantLock();
    Condition move = lock.newCondition();

    public void setNextMove(Coordinates nextMove) {
        lock.lock();
        try {
            this.nextMove = nextMove;
            move.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public Coordinates getNextMove() {
        lock.lock();

        try {
            while (nextMove == null) {
                move.await();
            }
        } catch (InterruptedException e) {
            System.out.println("InterruptedException thrown: " + e);
        } finally {
            lock.unlock();
        }

        Coordinates temp = nextMove;
        nextMove = null;
        return temp;
    }
}
